<?php defined('SYSPATH') OR die('No direct access allowed.');

class Forum_Controller extends Backend_Controller {

public $template = 'forum/admin/main';
protected static $current_tab = '';
protected static $tabs = array
(
	// method		=> i18n key
	'category'	=> 'categories',
	'users'			=> 'users',
);

  public function __construct() {
		parent::__construct();
		$this->template->head['css']['media/css/forum.css'] = 'all';
		$this->template->nav = View::factory('forum/admin/layout/nav')
			->bind('current', self::$current_tab)
			->bind('tabs', self::$tabs);
		$this->template->header = View::factory('forum/layout/header');
		$this->template->footer = new View('forum/layout/footer');
  }

  public function index() {
    $this->template->head['title'] = $this->template->title = Kohana::lang('admin/forum.title');
		$this->template->content = new View('forum/admin/default');
  }

  public function add() {
		$this->history = FALSE;
		// only for POST data
		$post = $this->input->post() OR Event::run('system.404');
		$category = ORM_MPTT::factory('forum_category');
		if ($category->validate($post)) {
			$parent = (isset($post['parent']) ? intval($post['parent']) : FALSE);
			if ($parent) {
				// Добавляем категорию в уже существующую
				if ($category->make_child($parent)) {
					message::add(Kohana::lang('admin/category.add.result_ok'), 'success');
					Event::run('forum.after.create.category', $category);
					url::redirect(Router::uri('admin', array('controller'=>'forum', 'method'=>'category'))."?parent=".$parent);
				}
				else {
					message::add(Kohana::lang('admin/category.add.result_error'), 'error');
					message::add($this->input->post(), 'custom');
					url::redirect($this->get_referrer());
				}
			}
			else {
				// создается категория в корне
				$category->make_root();
				Event::run('forum.after.create.category', $category);
				message::add(Kohana::lang('admin/category.add.result_ok'), 'success');
				url::redirect(Router::uri('admin', array('controller'=>'forum', 'method'=>'category')));
			}
		}
		else {
			message::add_validation($post->errors(), 'admin/category');
			message::add($this->input->post(), 'custom');
			url::redirect($this->get_referrer());
		}
  }

	public function category() {
		$parent = $this->input->get('parent', null);
		if (!is_null($parent)) {
			$parent = ORM_MPTT::factory('forum_category', $parent);
			$categories = $parent->children;
		}
		else {
			$categories = ORM_MPTT::factory('forum_category')->get_root();
		}
		$page = new View('forum/admin/manage_categories');
		$page->categories = $categories;
		$page->parent = $parent;
		self::$current_tab = 'category';
		$page->breadcrumb = (is_null($parent) ? array() : $parent->parents);
		$this->template->head['title'] = Kohana::lang('admin/category.category_management');
		$this->template->content = $page;
	}

	public function users() {
		// show all users
	}

	public function user() {
		// show applied user info
	}

  public function edit() {
		$this->history = FALSE;
  }

  public function delete() {
		$this->history = FALSE;
  }

}